<?php

//Load Settings from ini
$ini_array = parse_ini_file("tester.ini");

$inputDirectory     = $ini_array['inputDirectory'];
$outpotDirectory    = $ini_array['outpotDirectory'];
$expectedResultsDir = $ini_array['expectedResultsDir'];
$programName        = $ini_array['programName'];

#######################################
### TESZTESETEK LEFUTTATÁSA
#######################################

//Read file lists from directory

echo "\r\n" . 'Searching for test cases in "./' . $inputDirectory . '/" directory:' . "\r\n\r\n"; 

	#Input dir
	 
		 // create a handler for the directory
		$handler = opendir(getcwd() . '\\' .  $inputDirectory);
	
		// open directory and walk through the filenames
		while ($file = readdir($handler))
			{
				  // if file isn't this directory or its parent, add it to the results
				  if ($file != "." && $file != "..")
					$inputFiles[] = $file;
		  	}
			
//Execute input files on program, and save to output


foreach($inputFiles as $inputFile)
	{
		
		echo 'Test case found: ' . $inputFile . "\r\n";
	}
	
echo "\r\n\r\nExecuting test cases on program:\r\n\r\n\r\n";

foreach($inputFiles as $inputFile)
	{
	
		//Generate command to rub
		$command = $programName . ' < ' . getcwd() . '\\' .  $inputDirectory . '\\' . $inputFile;
	
		echo $command . "\r\n";
		
		//Run command		
		$result = shell_exec($command);
		
	
		//Save result to file		
		
		$resultPath = getcwd() . '\\' . $outpotDirectory . '\\' . $inputFile . '.result';
		$resultFilePaths[] = $resultPath;
		
		$fp = fopen($resultPath,"w");
		fwrite($fp,$result);
		fclose($fp);
		
		echo "\r\n\t STDOUT saved to: " . $outpotDirectory . '\\' . $inputFile . '.result' . "\r\n\r\n";
	}


#######################################
### REPORT GENERÁLÁSA
#######################################

echo "\r\n\r\nCompare results with expected results:\r\n\r\n\r\n";

foreach($resultFilePaths as $resultfilePath)
	{
		$expectedResultPath = str_replace($outpotDirectory,$expectedResultsDir,$resultfilePath);
		$expectedResultPath = str_replace('.result','.expected',$expectedResultPath);
		
		echo "Comparing $resultfilePath with $expectedResultPath \r\n";
		
		$result 	    = file_get_contents($resultfilePath);
		$expectedResult = file_get_contents($expectedResultPath);
		
		###########################
		## KIMENETEK REDUKÁLÁSA
		###########################
		
		//Kommentek eltavolitasa
		$result = preg_replace('(//.+)', '', $result);
		$expectedResult = preg_replace('(//.+)', '', $expectedResult);
		
		//WhiteSpacek eltavolitasa
		$result = preg_replace('/\s+/', '', $result);
		$expectedResult = preg_replace('/\s+/', '', $expectedResult);
		
		//kisnagybetu eltavolitasa
		$result = strtolower($result);
		$expectedResult = strtolower($expectedResult);
		

		###########################
		## KIMENETEK REDUKÁLÁSA VÉGE
		###########################
		
		if($result == $expectedResult)
			{
				echo "\r\n\t The two files are exactly the same! \r\n\r\n";
				$testCaseResult[] = "OK";
			}
		else
			{
				echo "\r\n\t The two files are NOT exactly the same!\r\n\r\n";	
				$testCaseResult[] = "FAIL";
			}
	
		
		
	}
	
######################################
## HTML REPORT KIÍRÁSA
######################################


$pre = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Teszteset jegyzőkönyv</title>
<style type="text/css">
body {
	background-color: #FFC;
}
</style>
  

<script type="text/javascript" src="./vendor/fancybox/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="./vendor/fancybox/jquery.lightbox-0.5.js"></script>
  
</head>

<body>';

$after = "
</body>
</html>
";

$title = '<h1 style="margin-bottom:30px;">A teszetlés lefutott ' . count($inputFiles) . 'db teszteseten ' . date("r") . ' időpontban:</h1>
<table border="0" cellspacing="10" cellpadding="0" style="margin-bottom:20px;">
  <tr>
    <td><div style="width:30px;height:30px; background-color:#dbf9c4; border:1px solid black;"></div></td>
    <td><div style="margin-left:10px; font-weight:bolder;">Sikeres teszteset</div></td>
        <td><div style="width:30px;height:30px; background-color:#ec4444; border:1px solid black;"></div></td>
    <td><div style="margin-left:10px; font-weight:bolder;"><u>Sikertelen</u> teszteset</div></td>
  </tr>
</table>
';

$body = "";

for($i=0;$i<count($testCaseResult);$i++)
	{
		$body .=
				'<div style="background-color:' . (($testCaseResult[$i] == "OK") ? "#dbf9c4" : "#ec4444") . '; border:1px black dotted; width:450px; margin-bottom:20px;">
				  <div style="border-bottom:1px solid black; background-color:#dc8e32; margin-bottom:10px; font-size:20px; font-weight: bolder; padding:5px;">
				  ' . ($i+1) . '. számú teszteset eredménye:</div>
				  <div style="padding:10px; font-weight: bolder;">
				  
				  ' . (($testCaseResult[$i] == "OK") ? "A teszteset kimenete megegyezett az elvárttal" : "A kimenet nem egyezett meg az elvárttal...") . '
				  
				  
				  <div style="border:2px solid black; background-color:#FFC; padding:5px; margin-top:10px;">
				  <a class="openInFancyBox" href="' . $inputDirectory . '/' . $inputFiles[$i] . '" target="_blank">A teszteset bemenete</a><br/>
				  <a class="openInFancyBox" href="' . $outpotDirectory . '/' . $inputFiles[$i] . '.result' . '" target="_blank">A teszteset kimenete</a><br/>
				  <a class="openInFancyBox" href="' . $expectedResultsDir . '/' . $inputFiles[$i] . '.expected' . '" target="_blank">A teszteset elvárt kimenete</a>
				  </div>
				  
				  </div>
				</div>
				
				
				<script type="text/javascript" src="./vendor/fancybox/jquery.mousewheel-3.0.4.pack.js"></script>
				<script type="text/javascript" src="./vendor/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
				<link rel="stylesheet" type="text/css" href="./vendor/fancybox/jquery.fancybox-1.3.4.css" media="screen" />

<script type="text/javascript">
		$(document).ready(function() {

			$(".openInFancyBox").fancybox({' ."
				'width'				: '90%',
				'height'			: '75%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'" . '
			});

		});
      </script>
				
				
				';
	}
	
$fp = fopen("result.html","w");
fwrite($fp,$pre . $title . $body . $after);
fclose($fp);

 echo "\r\n\r\nReport saved to: result.html \r\n\r\n\r\n";

?>